<template>
	<view class="my">
		<NavBar title="我的"></NavBar>
		<view class="content">

			<div class="user" v-if="store.userInfo.username" @click="$gotoUrl('/pages/my/setting')">
				<image :src="store.userInfo.avatar" mode="aspectFill"></image>
				<div class="cont">
					<div class="name">{{ store.userInfo.username }}</div>
					<div class="id">
						<image src="@/static/icons/id.png" mode="aspectFill"></image>
						{{ store.userInfo.nickname }}
					</div>
				</div>
			</div>
			<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" class="loginBtn" v-else>用户未登录，请先登录 </button>
			<div class="money">
				<div class="head">
					<div class="title">我的资产</div>
					<image @click="isShowIncome = !isShowIncome" src="@/static/icons/eye.png" alt=""></image>
				</div>
				<div class="numList" @click="$gotoUrl('/pages/my/incomeDetail')">
					<div class="item">
						<div class="title">今日收益金额（元）</div>
						<div v-if="isShowIncome" class="num">¥ {{ userBasicsInfo.day_earnings_amount }}</div>
						<div v-else class="num">¥ ********</div>
					</div>
					<div class="item">
						<div class="title">累计收益金额（元）</div>
						<div v-if="isShowIncome" class="num">¥ {{ userBasicsInfo.Accumulated_revenue_amount }}</div>
						<div v-else class="num">¥ ********</div>
					</div>
				</div>
			</div>
			<div class="card">
				<div class="title">我的订单</div>
				<div class="cardList">
					<div class="item" v-for="(item, index) of orders" @click="$gotoUrl(item.url)" :key="index">
						<image :src="item.img" alt="" mode="aspectFit"></image>
						<div class="text">{{ item.text }}</div>
					</div>
				</div>
			</div>
			<div class="card">
				<div class="title">分享中心</div>
				<div class="helpList">
					<div class="item">
						<div class="title">{{ userBasicsInfo.total_order }}</div>
						<div class="text">全部订单</div>
					</div>
					<div class="item">
						<div class="title" @click="$gotoUrl('/pages/my/individualOrder')">{{ userBasicsInfo.personal_orders }}</div>
						<div class="text">个人订单</div>
					</div>
					<div class="item">
						<div class="title" @click="$gotoUrl('/pages/my/serviceProviderOrder')">{{ userBasicsInfo.server_orders }}
						</div>
						<div class="text">服务商订单</div>
					</div>
					<div class="item" @click="$gotoUrl('/pages/my/clientele')">
						<div class="title">{{ userBasicsInfo.team_num }}</div>
						<div class="text">我的团队</div>
					</div>
				</div>
			</div>
			<div class="card">
				<div class="title">服务商帮助</div>
				<div class="serverList">
					<template v-for="(item, index) of servers">
						<div class="item" @click="$gotoUrl(item.url)" :key="index" v-if="handleShow(item.text)">
							<image :src="item.img" alt="" mode="aspectFit"></image>
							<div class="text">{{ item.text }}</div>
						</div>
					</template>
				</div>
			</div>
		</view>
	</view>
</template>
<script setup>
import { reactive, toRefs, onBeforeMount } from 'vue'
import { isserver, getOpenid, userWxlogin, userInfo, isServerRecomm } from '@/api/my.js'
import { UserStore } from '@/stores/user.js'
const store = UserStore()
const state = reactive({
	phoneCode: '',
	isShowIncome: true,
	orders: [{
		img: "/static/my/order1.png",
		text: "待付款",
		url: '/pages/order/order'
	},
	{
		img: "/static/my/order2.png",
		text: "待发货",
		url: '/pages/order/order'
	},
	{
		img: "/static/my/order3.png",
		text: "待收货",
		url: '/pages/order/order'
	},
	{
		img: "/static/my/order4.png",
		text: "退款/售后",
		url: '/pages/order/order'
	},
	{
		img: "/static/my/order5.png",
		text: "全部订单",
		url: '/pages/order/order'
	},
	],
	servers: [{
		img: "/static/my/server1.png",
		text: "推荐服务商",
		url: '/pages/services/serviceProviders'
	},
	{
		img: "/static/my/server2.png",
		text: "服务商中心",
		url: '/pages/services/serviceProviderCenter'
	},
	{
		img: "/static/my/server3.png",
		text: "服务点中心",
		url: "/pages/services/servicePointCenter"
	},
	{
		img: "/static/my/server4.png",
		text: "奖励池",
		url: "/pages/my/rewardPool"
	},
	{
		img: "/static/my/server5.png",
		text: "邀新有礼",
		url: "/pages/my/invite"
	},
	{
		img: "/static/my/server6.png",
		text: "客服中心"
	},
	{
		img: "/static/my/server7.png",
		text: "地址管理",
		url: '/pages/my/address'
	}
	],
	isServerInfo: {},
	isServerRecomm: {},
	userBasicsInfo: {
		Accumulated_revenue_amount: 0,
		Pending_settlement_amount: 0,
		day_earnings_amount: 0,
		personal_orders: 0,
		server_orders: 0,
		team_num: 0,
		total_order: 0,
	},
})
//获取手机号
async function getPhoneNumber (e) {
	console.log("🚀 ~ getPhoneNumber ~ e:", e)
	if (e.detail.code) {
		state.phoneCode = e.detail.code
		userLogin()
	} else {
		uni.showToast({
			title: '手机号授权失败',
			icon: 'none',
			duration: 2000
		})
	}

}

//用户登录
function userLogin () {
	uni.login({
		provider: 'weixin',
		success: async (e) => {
			// uni.getUserProfile({
			// 	desc: 'Wexin', // 必填，声明获取用户个人信息后的用途
			// 	success: (res) => {
			// 		console.log("🚀 ~ getUserInfo ~ res:", res)
			// 		userLogin(res.userInfo);
			// 	},
			// 	fail: (err) => {
			// 		console.log('用户信息获取失败', err);
			// 	}
			// });
			//获取openid
			const res = await getOpenid({
				code: e.code
			})
			//登录
			const resLogin = await userWxlogin({
				openid: res.data?.openid,
				code: state.phoneCode
			})
			//保存用户信息
			uni.setStorageSync('token', resLogin.data.userinfo.token)
			uni.setStorageSync('user_info', JSON.stringify(resLogin.data.userinfo))
			store.setUserInfo(resLogin.data.userinfo)
			//刷新用户权限
			getIsServer()
			uni.showToast({
				title: "登录成功"
			})
		},
		fail: (err) => {
			console.log('登录失败', err);
		}
	});
}

// 获取用户信息
async function getuserBasicsInfo () {
	const res = await userInfo()
	state.userBasicsInfo = res.data
	console.log(res, 'res')
}

//判断是否服务商
async function getIsServer () {
	const res = await isserver()
	state.isServerInfo = res.data
}

//是否推荐服务商隐藏
async function getIsServerRecomm () {
	const res = await isServerRecomm()
	state.isServerRecomm = res.data
}

//服务商显示隐藏
function handleShow (text) {
	if (text === '服务商中心') {
		return state.isServerInfo.Servers
	}
	if (text === '奖励池') {
		return state.isServerInfo.Servers
	}
	if (text === '服务点中心') {
		return state.isServerInfo.serviceProvidersUser
	}

	if (text === '推荐服务商') {
		return state.isServerRecomm.code
	}
	//其他放行
	return true
}






onBeforeMount(() => {
	getuserBasicsInfo()
	getIsServer()
	getIsServerRecomm()
})

const { orders, servers, userBasicsInfo, isServerInfo, isShowIncome } = toRefs(state)
</script>
<style lang="scss" scoped>
.content {
	padding: 36rpx;
	position: relative;
	z-index: 1;

	.loginBtn {
		background: #58DBCF;
	}

	.user {
		display: flex;
		align-items: center;

		&>image {
			width: 155rpx;
			height: 155rpx;
			border-radius: 50%;
			margin-right: 26rpx;
		}

		.cont {
			.name {
				font-weight: bold;
				font-size: 32rpx;
				color: #333333;
				line-height: 38rpx;
				margin-bottom: 8rpx;
			}

			.id {
				font-weight: 500;
				font-size: 26rpx;
				color: #333333;
				line-height: 30rpx;
				display: flex;
				align-items: center;

				image {
					width: 43rpx;
					height: 26rpx;
					margin-right: 5rpx;
				}
			}
		}
	}

	.setting {
		padding-left: 36rpx;
		font-weight: bold;
		font-size: 30rpx;
		color: #333333;
		line-height: 35rpx;
	}

	.money {
		padding: 31rpx 22rpx 54rpx 34rpx;
		background: #FFFFFF;
		border-radius: 22rpx 22rpx 22rpx 22rpx;
		margin-top: 43rpx;

		.head {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-bottom: 41rpx;

			.title {
				font-weight: bold;
				font-size: 30rpx;
				color: #333333;
				line-height: 35rpx;
			}

			image {
				width: 32rpx;
				height: 18rpx;
			}
		}

		.numList {
			display: flex;


			.item {
				flex: 1;
				text-align: center;

				.title {
					font-weight: 500;
					font-size: 26rpx;
					color: #333333;
					line-height: 30rpx;
				}

				.num {
					font-weight: 800;
					font-size: 30rpx;
					color: #000000;
					line-height: 35rpx;
					margin-top: 24rpx;
				}
			}

			.item+.item {
				border-left: 1px solid #F5F5F5;
			}
		}

		// border: 1rpx solid;
		// border-image: linear-gradient(180deg, rgba(88, 219, 207, 1), rgba(255, 255, 255, 0)) 1 1;
	}

	.card {
		background: #FFFFFF;
		border-radius: 22rpx 22rpx 22rpx 22rpx;
		padding: 22rpx;
		margin: 36rpx 0;

		&>.title {
			font-weight: bold;
			font-size: 26rpx;
			color: #333333;
			line-height: 30rpx;
			margin-bottom: 30rpx;
		}

		.cardList {
			display: flex;

			.item {
				flex: 1;
				text-align: center;

				image {
					width: 58rpx;
					height: 58rpx;
				}

				.text {
					margin-top: 10rpx;
				}
			}
		}

		.helpList {
			display: flex;
			margin-bottom: 30rpx;

			.item {
				flex: 1;
				text-align: center;

				.title {
					font-weight: bold;
					font-size: 30rpx;
					color: #000000;
					line-height: 35rpx;
				}

				.text {
					font-weight: 500;
					font-size: 20rpx;
					color: #333333;
					line-height: 23rpx;
					margin-top: 20rpx;
				}
			}
		}

		.serverList {
			display: flex;
			flex-wrap: wrap;

			.item {
				margin-bottom: 24rpx;
				width: 25%;
				text-align: center;

				image {
					width: 58rpx;
					height: 58rpx;
				}

				.text {
					font-weight: 500;
					font-size: 20rpx;
					color: #333333;
					line-height: 23rpx;
					margin-top: 10rpx;
				}
			}
		}
	}
}
</style>